<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="店铺名" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入店铺名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
<!--        <el-button type="success" plain icon="el-icon-edit" size="mini" @click="handleUpdate">批量设置结算时间</el-button>-->
      </el-form-item>
    </el-form>
     <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
       <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺名" align="left" prop="shopName" width="140" :show-overflow-tooltip="true" />
      <el-table-column label="用户名" align="left" prop="userName" width="140" :show-overflow-tooltip="true" />
       <el-table-column label="创建时间" align="center" prop="createTime" width="160">
         <template slot-scope="scope">
           <span>{{ parseTime(scope.row.createTime, '{m}/{d}/{y}') }}</span>
         </template>
       </el-table-column>
      <el-table-column label="结算时间" align="center" prop="settlementDay" width="140" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="max-operate-tab" width="140">
        <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">结算时间设置</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <el-dialog title="设置结算时间" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" label-position="left">
        <el-form-item label="订单结算时间" prop="settlementDay" :rules="[{ required: true, message: '请输入结算时间', trigger: 'blur' }]">
          <el-input class="modal-input" v-model="form.settlementDay" placeholder="请输入"/>
          <span>天</span>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="24">涉及店铺个{{ids.length || 1}}个</el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listList ,setTime} from "@/api/store/settlementCycle";
import DateRangePicker from '@/components/DateRangePicker/index.vue'
export default {
  name: "cycle",
  components: { DateRangePicker },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户小店表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    if(this.$route.query.shopName){
      this.queryParams.shopName = this.$route.query.shopName;
    }
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(response => {
        this.dataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this. queryParams= {
        pageNum: 1,
          pageSize: 20,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改设置时间 */
    handleUpdate(row) {
      this.reset();
      const id = [row.id] || this.ids
      this.open = true;
      this.form.shopIds = id;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          setTime(this.form).then(response => {
              this.$modal.msgSuccess("设置成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
  }
};
</script>

<style lang="scss" scope>
</style>
